package Client;

import Message.AccountAddFriendInfo;
import Message.AccountSendMessageInfo;
import utils.AccountAddFriendInfoListOfAll;
import utils.AccountSendMessageInfoFromOneList;
import utils.AccountAddFriendInfoList;
import utils.ReadWriteFileByAccountSendMessageInfoFromOneList;

import java.io.*;
import java.net.Socket;

/**
 * 该类应由登陆成功后的User类调用，初始化该类需要当前用户的UserId，一旦初始化该类就开始向服务端轮询有没有好友申请，
 * 该类的accountAddFriendInfoListOfAll属性是用来传入线程类中，组织每次轮询获得的好友申请。
 * 使用本类的getAccountAddFriendInfoListOfAll即可获得该链表，该链表的每个节点是AccountAddFriendInfoList，
 * 而AccountAddFriendInfoList的每个节点是AccountAddFriendInfo，即每条好友申请
 * 每种链表都可通过get方法获得其节点
 * 调用该类的interruptThread方法即可停止轮询，即退出登录时应调用该方法
 * */
public class SendRequestsRegularlyToGetFriendRequest extends Thread{
    private String userId;
    AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll;
    private ThreadHandleSendRequestsRegularlyToGetFriendRequest threadHandleSendRequestsRegularlyToGetFriendRequest;

    //调用该类的interruptThread方法即可停止轮询,退出登录的事件监听使用该方法
    public void interruptThread() {
        threadHandleSendRequestsRegularlyToGetFriendRequest.interrupt();
    }

    public AccountAddFriendInfoListOfAll getAccountAddFriendInfoListOfAll() {
        return accountAddFriendInfoListOfAll;
    }


    public SendRequestsRegularlyToGetFriendRequest(String userId, AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll) throws IOException{
        this.userId = userId;
        this.accountAddFriendInfoListOfAll = accountAddFriendInfoListOfAll;
        threadHandleSendRequestsRegularlyToGetFriendRequest = new ThreadHandleSendRequestsRegularlyToGetFriendRequest(userId, accountAddFriendInfoListOfAll);
        threadHandleSendRequestsRegularlyToGetFriendRequest.start();

        System.out.println("当前共有"+(Thread.activeCount()-2)+"个用户线程");//本地作为服务端时要减去两个服务器线程
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        String userId = "123455443";
        AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll = new AccountAddFriendInfoListOfAll();
        SendRequestsRegularlyToGetFriendRequest sendRequestsRegularlyToGetFriendRequest = new SendRequestsRegularlyToGetFriendRequest(userId, accountAddFriendInfoListOfAll);
        System.out.println("sleep before");
        sendRequestsRegularlyToGetFriendRequest.sleep(5000);
        sendRequestsRegularlyToGetFriendRequest.getAccountAddFriendInfoListOfAll();
        System.out.println("sleep after");
        for(int i=0;i<accountAddFriendInfoListOfAll.size();i++){
            for (int j=0;j<accountAddFriendInfoListOfAll.get(i).size();j++){
                System.out.println("收到好友申请" + i + ": " +accountAddFriendInfoListOfAll.get(i).get(j).getInfo());
            }
        }
    }
}

class ThreadHandleSendRequestsRegularlyToGetFriendRequest extends Thread{
    private String userId;
    private AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll;

    public ThreadHandleSendRequestsRegularlyToGetFriendRequest(String userId, AccountAddFriendInfoListOfAll accountAddFriendInfoListOfAll) throws IOException {
        super("用户" + userId + "的线程");
        this.userId = userId;
        this.accountAddFriendInfoListOfAll = accountAddFriendInfoListOfAll;
    }

    public String getUserId() {
        return userId;
    }

    public void run() {
        while (true) {
            if (Thread.currentThread().isInterrupted()) {
                System.out.println(this.getName()+"线程被中断");
                //处理中断逻辑
                break;
            } else {
                try {
                    Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_SENDREQUESTSREGULARLYTOGETFRIENDREQUEST_PORT);
                    OutputStream outputStream = socket.getOutputStream();//获取一个输出流，向服务端发送信息
                    PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流
                    printWriter.println(getUserId());//此处将当前用户的id发送给服务端
                    printWriter.flush();
                    //socket.shutdownOutput();//关闭输出流

                    InputStream inputStream = socket.getInputStream();
                    ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                    //下面这一行接收服务端返回的发送给当前用户的消息队列
                    AccountAddFriendInfoList accountAddFriendInfoList = (AccountAddFriendInfoList) objectInputStream.readObject();

                    //关闭响应资源
                    outputStream.close();
                    printWriter.close();
                    inputStream.close();
                    objectInputStream.close();
                    socket.close();

                    int size = accountAddFriendInfoList.size();
                    if(size == 0){
                        System.out.println("没有人发好友申请消息给当前用户");
                    }else{
                        accountAddFriendInfoListOfAll.add(accountAddFriendInfoList);
                    }
                    System.out.println("线程没有中断");
                    sleep(2000); //两秒向服务器申请获得消息
                } catch (InterruptedException | IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}